<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class ShclassController extends Controller {

    function __construct()
    {
        parent::__construct();
        $this->check_auth(); //检查权限

        $sh_class = M("shclass");
        $sh_class_array = $sh_class->select();
        $sh_grade = M("grade");
        $sh_grade_array = $sh_grade->where("is_inschool=0")->select();

        $this-> assign('sh_class_array',$sh_class_array);
        $this-> assign('sh_grade_array',$sh_grade_array);

    }

    public function showAll(){
        $stu_model=M('shclass');

        //导入分页助手类
        import('Org.Util.Page');
        $total= $stu_model->count();//记录总数
        $page=new Page($total,50);
        $page->setConfig("header",'个用户');
        $pageControl=$page->show();
        $stus=$stu_model->limit("$page->firstRow,$page->listRows")->order("id desc")->select();
        $this-> assign('stus',$stus);
        $this-> assign('page_title',"班级列表");
        $this-> assign('pageControl', $pageControl);
        $this-> display();

    }
    //实现学生修改功能
    public function update()
    {
        header("Content-Type:text/html;charset=utf-8");
        $stu=M('shclass');
        $id=$_GET['id'];
        if($id){
            //修改
            $stu_list=$stu->where("id=$id")->save($_POST);
            if($stu_list){
                $this->success('修改成功！！');
                $this->redirect('showAll');
            }else{
                $this->error('修改失败！！');
            }
        }else{
            $stuinfo['name']=$_POST['name'];
            $stuinfo['is_inschool']=$_POST['is_inschool'];
            $result = $stu->add($stuinfo);
            if($result)
            {
                $this->success('添加成功！！');
                $this->redirect('showAll');
            }else{
                $this->error('添加失败！！');
            }
        }

    }

    public function showUpdate()
    {

        $stu=M('shclass');
        $id=$_GET['id'];
        $stu_list=$stu->where("id=$id")->find();
      //  var_dump($stu_list);
        $this-> assign('page_title',"修改");
        $this->assign('stu_list',$stu_list);
        $this->display('update');
    }

    //实现学生删除功能
    public function del()
    {
        $stu=M('shclass');
        $id=$_GET['id'];
        $stu->where("id=$id")->delete();
        $this->redirect('showAll');//重定向到控制器的方法  display是本控制的方法
    }
    //实现学生添加功能

    public function showAdd()
    {
        $this-> assign('page_title',"添加");
        $this->display('update');

    }

}